class UserCampaignObserver < ActiveRecord::Observer
  
  def after_create(user_campaign)
    update_active(user_campaign)  
  end
  
  def after_update(user_campaign)
    update_active(user_campaign) 
  end
  
  private
  
  def update_active(user_campaign)
    if user_campaign.is_active
      ucs = UserCampaign.find_all_by_user_id_and_is_active(user_campaign.user.id,true)
      for uc in ucs do
        if uc.id != user_campaign.id
          uc.is_active = false
          uc.save!
        end  
      end  
    end
  end
  
end
